import path from "node:path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite";
import legacy from "@vitejs/plugin-legacy";
import { compression } from "vite-plugin-compression2";

const plugins = [
  vue(),
  VueI18nPlugin({
    include: [path.resolve(__dirname, "./src/i18n/**/*.json")],
    // include: [path.resolve(__dirname, "./src/i18n/zh-cn.json")],
  }),
  legacy({
    // defaults already drop IE support
    targets: ["defaults"],
  }),
  // compression({ include: /\.js$/i, deleteOriginalAssets: true }),
];

const resolve = {
  alias: {
    // vue: "@vue/compat",
    "@/": `${path.resolve(__dirname, "src")}/`,
  },
};

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  if (command === "serve") {
    return {
      plugins,
      resolve,
      base: "/netdrive/",
      server: {
        proxy: {
          "/api/command": {
            target: "http://10.1.100.124:7878",
            changeOrigin: true,
            secure: false,
            ws: true,
          },
          "/api": {
            target: "http://10.1.100.124:7878",
            changeOrigin: true,
            secure: false,
            ws: true,
          },
          "/storage": {
            target: "http://10.1.100.124:7878",
            changeOrigin: true,
            secure: false,
            ws: true,
            // rewrite: (path) => path.replace(/^\/storge/, '')
          },
        },
      },
    };
  } else {
    // command === 'build'
    return {
      plugins,
      resolve,
      base: "/netdrive/",
      // build: {
      //   rollupOptions: {
      //     // input: {
      //     //   index: path.resolve(__dirname, "./public/index.html"),
      //     // },
      //     output: {
      //       manualChunks: (id) => {
      //         // bundle dayjs files in a single chunk
      //         // this avoids having small files for each locale
      //         if (id.includes("dayjs/")) {
      //           return "dayjs";
      //           // bundle i18n in a separate chunk
      //         } else if (id.includes("i18n/")) {
      //           return "i18n";
      //         }
      //       },
      //     },
      //   },
      // },
      // experimental: {
      //   renderBuiltUrl(filename, { hostType }) {
      //     if (hostType === "js") {
      //       return { runtime: `window.__prependStaticUrl("${filename}")` };
      //     } else if (hostType === "html") {
      //       return `[{[ .StaticURL ]}]/${filename}`;
      //     } else {
      //       return { relative: true };
      //     }
      //   },
      // },
    };
  }
});
